package com.eqdd.yiqidian.ui.yiqiyue.fragment;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.YueHotArticleFragmentCustom;
import com.eqdd.yiqidian.adapter.BGABannerAdapter;
import com.eqdd.yiqidian.adapter.RecyclerViewHeader;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseFragment;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.ArticleMenuListBean;
import com.eqdd.yiqidian.bean.rebuild.HttpResultRows;
import com.eqdd.yiqidian.bean.rebuild.YueArticleListBean;
import com.eqdd.yiqidian.bean.rebuild.YueArticleListBean;
import com.eqdd.yiqidian.bean.rebuild.ZhangPageResultBean;
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource;
import com.eqdd.yiqidian.helper.mvchelper.Rx2DataSource;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.JsonConverter;
import com.eqdd.yiqidian.ui.discover.workcircle.WorkCircleActivity;
import com.eqdd.yiqidian.utils.RefreshUtil;
import com.eqdd.yiqidian.utils.SwipeUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okrx2.adapter.FlowableBody;
import com.shizhefei.mvc.MVCCoolHelper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.rong.imkit.utils.RongDateUtils;

/**
 * @author吕志豪 .
 * @date 17-12-6  下午2:33.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class YueFragment extends BaseFragment {
    YueHotArticleFragmentCustom dataBinding;
    private SlimAdapterEx slimAdapterEx;
    private int pageNum = 0;
    private String url;

    @Override
    protected void setView() {
    }

    @Override
    protected void initData() {
        if (slimAdapterEx == null) {
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
//            dataBinding.viewPager.setAdapter(new BGABannerAdapter(getActivity()));
//            dataBinding.viewPager.setData(resIds);
            dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
//            dataBinding.header.attachTo(dataBinding.recyclerView);
            slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_80, new SlimInjector<YueArticleListBean>() {
                @Override
                public void onInject(YueArticleListBean data, IViewInjector injector) {
                    try {
                        injector.image(R.id.iv_head, data.getIphoto())
                                .text(R.id.stv_name, data.getUpname())
                                .text(R.id.tv_depart, RongDateUtils.getConversationFormatDate(sf.parse(data.getCreateTime()).getTime(), getActivity()))
                                .image(R.id.iv_img, data.getImage())
                                .text(R.id.tv_content, data.getContent())
                                .visibility(R.id.iv_img, TextUtils.isEmpty(data.getImage().trim()) ? View.GONE : View.VISIBLE)
                                .text(R.id.tv_source, data.getLable())
                                .text(R.id.stv_msg, data.getCommentCount() + "")
                                .text(R.id.stv_look, data.getBrowseCount() + "")
                                .text(R.id.stv_support, data.getZanCount() + "");
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
            }).registerDefault(R.layout.list_item_01, new SlimInjector() {
                @Override
                public void onInject(Object data, IViewInjector injector) {
                    System.out.println(data);
                }
            }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());
//            dataBinding.header.setOnScrollTopListener(isTop -> dataBinding.coolRefreshView.setEnabled(isTop));
            ModelRx2DataSource<YueArticleListBean> dataSource = new ModelRx2DataSource<>(new ModelRx2DataSource.OnLoadSource() {
                @Override
                public Flowable<List> loadSourcce(int page, Rx2DataSource.DoneActionRegister register) {
                    pageNum = page == 1 ? 0 : pageNum;
                    return OkGo.<HttpResultRows<YueArticleListBean>>post(HttpConfig.BASE_URL + url)
                            .params("page", pageNum)
                            .converter(new JsonConverter<HttpResultRows<YueArticleListBean>>() {
                                @Override
                                public void test() {
                                    super.test();
                                }
                            }).adapt(new FlowableBody<>())
                            .flatMap(listResultBean -> {
                                pageNum = listResultBean.getItems().getPage();
                                if (listResultBean.getStatus() == 200) {
                                    return Flowable.just(listResultBean.getItems() == null ? new ArrayList<YueArticleListBean>() : listResultBean.getItems().getRows());
                                } else {
                                    ToastUtil.showShort(listResultBean.getMsg());
                                    return Flowable.just(new ArrayList<>());
                                }
                            });

                }
            });
            MVCCoolHelper<List<YueArticleListBean>> mvcHelper = new MVCCoolHelper<>(dataBinding.coolRefreshView);
            mvcHelper.setDataSource(dataSource);
            mvcHelper.setAdapter(slimAdapterEx);
            mvcHelper.refresh();
        }
    }


    @Override
    public ViewDataBinding initBinding(LayoutInflater inflater) {
        url = getArguments().getString(Config.URL);
        return dataBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_yue_hot_article, null, false);

    }

    @Override
    public void onClick(View v) {

    }


}
